/**
 * Created by Richard Radlherr.
 * Date: 09.07.2006 20:01
 */

using System;
using TagTool.Core;
using TagTool.GraphicTypes;

namespace TagTool.Nodes.Drawing
{
	public class TagRecorder : BaseNode
	{
		
		private InputSlot location;
		private InputSlot color;
		private InputSlot width;
		
		private InputSlot draw;
		private InputSlot newTag;
		private InputSlot clearTag;
		
		private OutputSlot outputTag;
		
		bool d = false;
		bool bNewTag = false;
		
		public TagRecorder()
		{
			Name = "Tag Recorder";
			location = new InputSlot(this, "Location", new PointD());
			location.ParseCallback = ParseHelper.ParseVector;
			
			color = new InputSlot(this, "Color", new Color(1.0, 1.0, 1.0, 1.0));
			color.ParseCallback = ParseHelper.ParseColor;
			
			width = new InputSlot(this, "Width", 0.1);
			width.ParseCallback = ParseHelper.ParseDouble;
			
			draw = new InputSlot(this, "Draw", false);
			newTag = new InputSlot(this, "New Tag", false);			
			clearTag = new InputSlot(this, "Clear Tag", false);
			
			outputTag = new OutputSlot(this, "Tag", typeof(Tag));
			outputTag.Value = new Tag();
		}
		
		public override void Initialize() {
			outputTag.Value = new Tag();
			Update(clearTag);
		}
		
		public override void Update(Object sender)
		{
			if ((bool)clearTag.Value)
			{
				((Tag)outputTag.Value).Clear();
			}
			else if ((bool)newTag.Value != bNewTag && !bNewTag)
			{
				// complete the tag
				((Tag)outputTag.Value).Update();
				((Tag)outputTag.Value).SetPivot();

				// signal readyness - connected inputs may take the data

				CastTrigger();
				this.TriggerOut.Value = false;
				// create the new Tag
				outputTag.Value = new Tag();
			}
			else
			{
				if (d != (bool)draw.Value && d == false)
				{
					((Tag)outputTag.Value).AddLine();
				}
				d = (bool)draw.Value;

				if (d)
				{
					Color co = (Color)color.Value;
					
					((Tag)outputTag.Value).AddPoint(new ColoredPoint((PointD)location.Value, new Color(co.A, co.R, co.G, co.B), (double)width.Value));
				}
			}
			bNewTag = (bool)newTag.Value;
		}
		
		[NodeAttribute(SlotType.Input, Show = true)]
		public InputSlot Location
		{
			get { return location; }
		}

		[NodeAttribute(SlotType.Input, Show = true)]
		public InputSlot Color
		{
			get { return color; }
		}
		
		[NodeAttribute(SlotType.Input, Show = true)]
		public InputSlot Width
		{
			get { return width; }
		}
		
		[NodeAttribute(SlotType.Input, Show = true)]
		public InputSlot Draw
		{
			get { return draw; }
		}
		
		[NodeAttribute(SlotType.Input, Show = true)]
		public InputSlot NewTag
		{
			get { return newTag; }
		}
		
		[NodeAttribute(SlotType.Input, Show = true)]
		public InputSlot ClearTag
		{
			get { return clearTag; }
		}
		
		[NodeAttribute(SlotType.Output, Show = true)]
		public OutputSlot Tag
		{
			get { return outputTag; }
		}
				
	}
}
